package com.ims.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ims.common.core.domain.entity.SysDept;
import com.ims.common.core.domain.entity.SysUser;
import com.ims.common.core.domain.model.LoginUser;
import com.ims.common.enums.UserStatus;
import com.ims.common.exception.base.BaseException;
import com.ims.common.utils.*;
import com.ims.system.mapper.SysUserMapper;
import com.ims.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import com.ims.system.mapper.TSysSiteMapper;
import com.ims.system.domain.TSysSite;
import com.ims.system.service.ITSysSiteService;

/**
 * 公司Service业务层处理
 *
 * @author IMS
 * @date 2024-05-25
 */
@Service
public class TSysSiteServiceImpl implements ITSysSiteService
{
    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private TSysSiteMapper tSysSiteMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询公司
     *
     * @param id 公司主键
     * @return 公司
     */
    @Override
    public TSysSite selectTSysSiteById(Long id)
    {
        return tSysSiteMapper.selectTSysSiteById(id);
    }

    /**
     * 获取当前企业
     * @return
     */
    @Override
    public TSysSite getCurrentSite() {
        // 当前登录人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String siteCode = loginUser.getSiteCode();

        return selectTSysSiteByCode(siteCode);
    }

    /**
     * 查询公司（编码）
     *
     * @param siteCode 公司编码
     * @return 公司
     */
    @Override
    public TSysSite selectTSysSiteByCode(String siteCode)
    {
        return tSysSiteMapper.selectTSysSiteByCode(siteCode);
    }

    /**
     * 查询公司列表
     *
     * @param tSysSite 公司
     * @return 公司
     */
    @Override
    public List<TSysSite> selectTSysSiteList(TSysSite tSysSite)
    {
        return tSysSiteMapper.selectTSysSiteList(tSysSite);
    }

    /**
     * 新增公司
     *
     * @param tSysSite 公司
     * @return 结果
     */
    @Override
    public int insertTSysSite(TSysSite tSysSite)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = sysUserMapper.selectUserById(loginUser.getUserId());
        TSysSite tSysSiteCondition = new TSysSite();
        tSysSiteCondition.setCustId(sysUser.getCustId());
        List<TSysSite> sysSites = tSysSiteMapper.selectTSysSiteList(tSysSiteCondition);
        tSysSite.setSiteCode(ImCodeUtils.getSiteCode(sysUser.getCustId(), sysSites.size()));

        if(StringUtils.isEmpty(tSysSite.getInvoiceTypes())){
            throw new BaseException("发票类型不能为空");
        }
        String invoiceTypeNames = "";
        for(String invoiceType : tSysSite.getInvoiceTypes().split(",")){
            invoiceTypeNames += "," + DictUtils.getDictLabel("invoice_type", invoiceType);
        }
        if(!StringUtils.isEmpty(invoiceTypeNames)){
            tSysSite.setInvoiceTypeNames(invoiceTypeNames.substring(1));
        }
        tSysSite.setPropertyTypeName(DictUtils.getDictLabel("property_type", tSysSite.getPropertyType()+""));

        tSysSite.setCreateBy(sysUser.getUserName());
        tSysSite.setCreateTime(DateUtils.getNowDate());
        return tSysSiteMapper.insertTSysSite(tSysSite);
    }

    /**
     * 修改公司
     *
     * @param tSysSite 公司
     * @return 结果
     */
    @Override
    public int updateTSysSite(TSysSite tSysSite)
    {
        //提交人
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tSysSite.setTaxDeptName(DictUtils.getDictLabel("tax_dept", tSysSite.getTaxDept() + ""));
        tSysSite.setPropertyTypeName(DictUtils.getDictLabel("property_type", tSysSite.getPropertyType() + ""));
        tSysSite.setUpdateBy(user.getUserName());
        tSysSite.setUpdateTime(DateUtils.getNowDate());
        int res = tSysSiteMapper.updateTSysSite(tSysSite);
        if(res == 1){
            // 更新对应的 部门表
            SysDept sysDept = sysDeptService.selectSiteDeptBySiteCode(tSysSite.getSiteCode());
            if(!Objects.isNull(sysDept)){
                sysDept.setSiteCode(tSysSite.getSiteCode());
                sysDept.setSiteName(tSysSite.getSiteName());
                sysDept.setDeptName(tSysSite.getSiteName());
                sysDept.setPhone(tSysSite.getTel());
                sysDeptService.updateDeptOnly(sysDept);
            } else {
                // 若没有对应的部门数据，则插入一条
                sysDept = new SysDept();
                sysDept.setSiteCode(tSysSite.getSiteCode());
                sysDept.setSiteName(tSysSite.getSiteName());
                sysDept.setDeptName(tSysSite.getSiteName());
                sysDept.setStatus(UserStatus.OK.getCode());
                sysDept.setPhone(tSysSite.getTel());
                sysDept.setAncestors("");
                sysDept.setParentId(null);
                sysDept.setDelFlag("0");
                sysDept.setOrderNum(0);
                sysDept.setCreateBy(user.getUserName());
                sysDept.setCreateTime(DateUtils.getNowDate());
                sysDeptService.insertDeptOnly(sysDept);
            }
        }
        return res;
    }

    /**
     * 批量删除公司
     *
     * @param ids 需要删除的公司主键
     * @return 结果
     */
    @Override
    public int deleteTSysSiteByIds(Long[] ids)
    {
        return tSysSiteMapper.deleteTSysSiteByIds(ids);
    }

    /**
     * 删除公司信息
     *
     * @param id 公司主键
     * @return 结果
     */
    @Override
    public int deleteTSysSiteById(Long id)
    {
        return tSysSiteMapper.deleteTSysSiteById(id);
    }

    @Override
    public TSysSite selectDefaultSysSite() {
        return tSysSiteMapper.selectDefaultSysSite();
    }
}
